package com.example.teamtribe.main.mine.sign;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import com.example.teamtribe.Application;
import com.example.teamtribe.common.Back;
import com.example.teamtribe.utils.HttpUtil;
import com.example.teamtribe.utils.URLStore;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class SignModel {

    private final String FILE = "sign";
    private final String IS_SIGN = "isSign";
    private final String TIME = "time";
    private final String DAYS = "getDays";
    private final String SAYING = "saying";

    public String getSaying() {
        SharedPreferences spf = Application.getContext().getSharedPreferences(FILE, Context.MODE_PRIVATE);
        return spf.getString(SAYING, "");
    }

    public void getSaying(String token, final Back back) {
        HttpUtil.post(URLStore.SIGN_SENTENCE, getParams(token), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("获取格言", str);
                try {
                    JSONObject object = new JSONObject(str);
                    int status = object.getInt("code");
                    if (status != 0) {
                        back.failure(HttpUtil.getCodeMeg(status));
                    } else {
                        Bundle bundle = new Bundle();
                        String saying = object.getString("sentence");
                        bundle.putString("saying", saying);
                        setSaying(saying);
                        back.success(bundle);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void sign(final String token, final Back back) {
        HttpUtil.post(URLStore.SIGN_SIGN, getParams(token), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("签到", str);
                try {
                    JSONObject object = new JSONObject(str);
                    int status = object.getInt("code");
                    if (status != 0 && status != 19) {
                        back.failure(HttpUtil.getCodeMeg(status));
                    } else {
                        setSign(true);
                        setDays(getDays() + 1);
                        back.success(null);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public boolean isSign() {
        SharedPreferences spf = Application.getContext().getSharedPreferences(FILE, Context.MODE_PRIVATE);
        return isToday() && spf.getBoolean(IS_SIGN, false);
    }

    public void isSign(String token, final Back back) {
        HttpUtil.post(URLStore.SIGN_IS, getParams(token), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("是否签到", str);
                try {
                    JSONObject object = new JSONObject(str);
                    int status = object.getInt("code");
                    Bundle bundle = new Bundle();
                    if (status == 18) {
                        bundle.putBoolean("isSign", true);
                        setSign(true);
                        back.success(bundle);
                    } else if (status == 19){
                        bundle.putBoolean("isSign", false);
                        setSign(false);
                        back.success(bundle);
                    } else {
                        back.failure(HttpUtil.getCodeMeg(status));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public int getDays() {
        SharedPreferences spf = Application.getContext().getSharedPreferences(FILE, Context.MODE_PRIVATE);
        return spf.getInt(DAYS, 0);
    }

    public void getDays(String token, final Back back) {
        HttpUtil.post(URLStore.SIGN_DAYS, getParams(token), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                back.failure(HttpUtil.getFailureMeg());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Log.d("获得天数", str);
                try {
                    JSONObject object = new JSONObject(str);
                    int status = object.getInt("code");
                    if (status != 0) {
                        back.failure(HttpUtil.getCodeMeg(status));
                    } else {
                        Bundle bundle = new Bundle();
                        int days = object.getInt("day");
                        bundle.putInt("getDays", days);
                        setDays(days);
                        back.success(bundle);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public boolean isToday() {
        SharedPreferences spf = Application.getContext().getSharedPreferences(FILE, Context.MODE_PRIVATE);
        int before = spf.getInt(TIME, 0);
        int now = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        return before == now;
    }

    private Map<String, String> getParams(String token) {
        Map<String, String> params = new HashMap<>(1);
        params.put("TeamTribeToken", token);
        return params;
    }

    private void setSaying(String saying) {
        SharedPreferences.Editor editor = Application.getContext().getSharedPreferences(FILE, Context.MODE_PRIVATE).edit();
        editor.putString(SAYING, saying);
        editor.apply();
    }

    public void setDays(int days) {
        SharedPreferences.Editor editor = Application.getContext().getSharedPreferences(FILE, Context.MODE_PRIVATE).edit();
        editor.putInt(DAYS, days);
        editor.apply();
    }

    private void setSign(boolean isSign) {
        SharedPreferences.Editor editor = Application.getContext().getSharedPreferences(FILE, Context.MODE_PRIVATE).edit();
        editor.putBoolean(IS_SIGN, isSign);
        editor.putInt(TIME, Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
        editor.apply();
    }

}
